package com.gzc.just.play.last.war.loginserver;

import com.gzc.just.play.last.war.common.handler.VirtualThreadNettyServer;
import com.gzc.just.play.last.war.common.handler.VirtualThreadServerFramework;
import com.gzc.just.play.last.war.loginserver.handler.*;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Login Server的Netty服务器实现
 * 使用虚拟线程处理登录请求
 */
@Component
public class LoginNettyServer extends VirtualThreadNettyServer {
    private static final Logger logger = LoggerFactory.getLogger(LoginNettyServer.class);
    
    @Value("${login.server.port:30001}")
    private int loginServerPort;
    
    @Value("${login.server.host:127.0.0.1}")
    private String loginServerHost;
    
    @Override
    protected int getNettyPort() {
        return loginServerPort;
    }
    
    @Override
    protected String getNettyHost() {
        return loginServerHost;
    }
    
    @Override
    protected VirtualThreadServerFramework.ServerType getServerType() {
        return VirtualThreadServerFramework.ServerType.LOGIN_SERVER;
    }
    
    @Override
    protected void initializeHandlers() {
        // 注册Login Server的消息处理器
        framework.registerHandler(new LoginMessageHandler());//todo 是否可以删除？
        framework.registerHandler(new LogoutMessageHandler());
        framework.registerHandler(new RegisterMessageHandler());
        framework.registerHandler(new TokenValidationHandler());
        framework.registerHandler(new PasswordResetHandler());
        
        logger.info("Initialized {} handlers for Login Server", framework.getHandlerManager().getHandlerCount());
    }
    
    @Override
    protected void onConnectionActive(ChannelHandlerContext ctx) {
        super.onConnectionActive(ctx);
        // Login Server特有的连接处理逻辑
        logger.debug("New login connection from: {}", ctx.channel().remoteAddress());
    }
    
    @Override
    protected void onConnectionInactive(ChannelHandlerContext ctx) {
        super.onConnectionInactive(ctx);
        // Login Server特有的连接关闭处理逻辑
        logger.debug("Login connection closed from: {}", ctx.channel().remoteAddress());
    }
}